import { resources, JsonAsset } from 'cc';

class Tools {
    /**
     * 加载 JSON 文件
     * @param fileName 文件名（不带后缀）
     * @returns 解析后的 JSON 数据
     */
    static loadJson(fileName: string): Promise<any> {
        return new Promise((resolve, reject) => {
            resources.load(fileName, JsonAsset, (err, jsonAsset) => {
                if (err) {
                    console.error(`Failed to load JSON file: ${fileName}`, err);
                    reject(err);
                    return;
                }
                const jsonData = jsonAsset?.json;
                if (!jsonData) {
                    console.error(`JSON data is undefined or null for file: ${fileName}`);
                    reject(new Error('JSON data is invalid'));
                    return;
                }
                resolve(jsonData);
            });
        });
    }

    static isAsyncFunction(fn: Function): boolean {
        return fn.constructor.name === "AsyncFunction";
    }
}

export default Tools;